package com.example.stuSysbackend.controller;


import com.example.stuSysbackend.domain.ApprovalRecords;
import com.example.stuSysbackend.domain.Result;
import com.example.stuSysbackend.service.ApprovalRecordsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/Approval")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "审批接口")
public class CommonController {
    @Autowired
    private ApprovalRecordsService appService;
    private String filePath;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        log.info("文件上传：{}", file.getOriginalFilename());
        try {
            String filePath = appService.saveFile(file);
            this.filePath = filePath;
            return Result.success("上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败");
        }
    }

    @PostMapping("/approval")
    @ApiOperation("提交申请人信息")
    public Result<String> submitApproval(@RequestBody ApprovalRecords app) {
        log.info("提交申请人信息：{}", app);
        app.setFile(filePath);
        appService.saveApp(app);
        try {
            appService.insert();
            return Result.success("提交成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("提交失败");
        }
    }

    @GetMapping("/queryApproval")
    @ApiOperation("查询申请信息")
    public List<ApprovalRecords> queryApproval(@RequestBody ApprovalRecords app) {
        log.info("查询申请信息：{}", app);
        List<ApprovalRecords> list = appService.queryApproval(app);
        return list;
    }

    @PutMapping("/update")
    @ApiOperation("审批修改")
    public Result<String> update(@RequestBody ApprovalRecords app, HttpServletRequest request) {
        log.info("审批修改：{}", app);
        try {
            HttpSession session = request.getSession();
            String username = (String) session.getAttribute("username");
            app.setApproval_personnel(username);
            app.setFile(filePath);
            appService.update(app);
            return Result.success("审批成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("审批失败");
        }
    }

    @PutMapping("/updateStatus1/#{id}")
    @ApiOperation("班委审批状态")
    public Result<String> updateupdateStatus1(@PathVariable Integer id) {
        log.info("班委审批：{}", id);
        try {
            appService.updateStatus1(id);
            return Result.success("审批成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("审批失败");
        }
    }

    @PutMapping("/updateStatus2/#{id}")
    @ApiOperation("教师审批状态")
    public Result<String> updateupdateStatus(@PathVariable Integer id) {
        log.info("教师审批：{}", id);
        try {
            appService.updateStatus2(id);
            return Result.success("审批成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("审批失败");
        }
    }

    @PutMapping("/back/#{id}")
    @ApiOperation("审批打回")
    public Result<String> updateBack(@PathVariable Integer id) {
        log.info("审批打回：{}", id);
        try {
            appService.updateBack(id);
            return Result.success("打回成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("打回失败");
        }
    }

    @GetMapping("/queryById/#{id}")
    @ApiOperation("根据Id回显")
    public Result<ApprovalRecords> queryById(@PathVariable Integer id) {
        log.info("根据Id回显：{}", id);
        ApprovalRecords approvalRecords = appService.selectById(id);
        return Result.success(approvalRecords);
    }

    @DeleteMapping("/deleteById/#{id}")
    @ApiOperation("根据Id删除")
    public Result<String> deleteById(@PathVariable Integer id) {
        log.info("根据Id删除：{}", id);
        appService.deleteById(id);
        return Result.success("删除成功");
    }
}